package com.usabb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ifast.common.base.CoreServiceImpl;
import com.usabb.dao.CombosCategoryDao;
import com.usabb.domain.CombosCategoryDO;
import com.usabb.domain.CombosCategoryItemDO;
import com.usabb.domain.MaternityBaseDO;
import com.usabb.pojo.vo.CombosCategoryAddVO;
import com.usabb.pojo.vo.CombosCategoryUpdateVO;
import com.usabb.service.CombosCategoryItemService;
import com.usabb.service.CombosCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Slf4j
@Service
public class CombosCategoryServiceImpl extends CoreServiceImpl<CombosCategoryDao, CombosCategoryDO> implements CombosCategoryService {

    @Autowired
    private CombosCategoryItemService combosCategoryItemService;

    @Override
    @Transactional
    public boolean addCategories(CombosCategoryAddVO addVO) {
        log.debug("Vo: {}", addVO);

        List<CombosCategoryDO> categoryDOS = CombosCategoryDO.from(addVO);
        this.insertBatch(categoryDOS);

        categoryDOS.forEach(it -> {
                    List<CombosCategoryItemDO> items = it.getItems();
                    items.forEach(itt -> itt.setCategoryId(it.getId()));

                    combosCategoryItemService.insertBatch(items);
                }
        );

        return true;
    }

    @Override
    @Transactional
    public boolean updateCategory(CombosCategoryUpdateVO updateVO) {
        final Long categoryId = updateVO.getId();
        CombosCategoryDO origin = selectById(categoryId);
        origin.setCategory(updateVO.getCategory());
        this.updateById(origin);

        final boolean delete = combosCategoryItemService.deleteByCategoryId(categoryId);

        final List<CombosCategoryItemDO> itemDOS = CombosCategoryItemDO.ofItems(updateVO);
        return combosCategoryItemService.insertBatch(itemDOS);
    }

    @Override
    public CombosCategoryDO getByIdWithItems(Long id) {
        CombosCategoryDO categoryDO = this.selectById(id);

        appendItems(categoryDO);

        return categoryDO;
    }

    @Override
    public List<CombosCategoryDO> getByBizIdWithItems(Long bizId) {
        final List<CombosCategoryDO> categoryDOS = getByBizId(bizId);
        categoryDOS.forEach(it->appendItems(it));

        return categoryDOS;
    }

    private void appendItems(CombosCategoryDO categoryDO) {
        List<CombosCategoryItemDO> itemDOS = combosCategoryItemService.getByCategoryId(categoryDO.getId());
        categoryDO.setItems(itemDOS);
    }

    private List<CombosCategoryDO> getByBizId(Long bizId) {
        CombosCategoryDO param = new CombosCategoryDO();
        param.setBaseId(bizId);
        Wrapper<CombosCategoryDO> wrapper = new EntityWrapper<>(param);
        return this.selectList(wrapper);
    }
}
